package admin

import (
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"mall-go/common"
	"mall-go/dto"
	"mall-go/model"
	"mall-go/repository"
	"mall-go/response"
	"mall-go/vo"
	"strconv"
)

type GoodsController struct {
	DB *gorm.DB
}

const (
	SellStatusUp   = 0
	SellStatusDown = 1
)

// List 查询商品列表
func (receiver GoodsController) List(ctx *gin.Context) {
	//获取分页数据
	pageNumber, _ := strconv.Atoi(ctx.Query("pageNumber"))
	pageSize, _ := strconv.Atoi(ctx.Query("pageSize"))
	goodsName := ctx.Query("goodsName")
	goodsSellStatus := ctx.Query("goodsSellStatus")
	if pageNumber < 1 || pageSize < 10 {
		panic("分页参数异常")
	}
	pageDTO := dto.GoodsPageDTO{
		Page:  pageNumber,
		Limit: pageSize,
		Start: (pageNumber - 1) * pageSize,
	}
	if goodsName != "" {
		//这里把keyword当成商品名称就行了，不想再重新写一个dto
		pageDTO.Keyword = goodsName
	}
	//这里没有想到有什么很好的处理零值的方案
	if goodsSellStatus != "" {
		status, _ := strconv.Atoi(goodsSellStatus)
		pageDTO.GoodsSellStatus = status
	}
	goodsRepo := repository.NewMallGoodsRepo()
	list := goodsRepo.GetNewBeeMallGoodsPage(pageDTO)
	total := goodsRepo.GetTotalNewBeeMallGoods(pageDTO)
	//构建分页vo
	page := vo.GoodsPageResult{
		PageResult: vo.PageResult{
			TotalCount: total,
			PageSize:   pageDTO.Limit,
			TotalPage:  total / pageDTO.Limit,
			CurrPage:   pageDTO.Page,
		},
		List: list,
	}
	response.Success(ctx, gin.H{"goodsVO": page}, "Success")
}

// Save 增加
func (receiver GoodsController) Save(ctx *gin.Context) {
	//取出管理员id
	user, _ := ctx.Get("adminUser")
	adminUserId := user.(model.NewBeeAdminUser).AdminUserId
	var param dto.GoodsAddParam
	if err := ctx.ShouldBind(&param); err != nil {
		panic(err)
	}
	goodsRepo := repository.NewMallGoodsRepo()
	flag := goodsRepo.SaveGoods(param, adminUserId)
	if flag {
		response.Success(ctx, nil, "Success")
	}
}

// Update 更新商品
func (receiver GoodsController) Update(ctx *gin.Context) {
	//取出管理员id
	user, _ := ctx.Get("adminUser")
	adminUserId := user.(model.NewBeeAdminUser).AdminUserId
	var param dto.GoodsEditParam
	if err := ctx.ShouldBind(&param); err != nil {
		panic(err)
	}
	goodsRepo := repository.NewMallGoodsRepo()
	flag := goodsRepo.UpdateNewBeeMallGoods(param, adminUserId)
	if flag {
		response.Success(ctx, nil, "修改成功")
	}
}

// Info 详情
func (receiver GoodsController) Info(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Params.ByName("id"))
	//根据主键获取商品详情
	result := make(map[string]interface{})
	var goods model.GoodsInfo
	receiver.DB.First(&goods, id)
	if goods.GoodsId == 0 {
		panic("数据不存在")
	}
	result["goods"] = goods
	//获取第三级商品分类
	goodsRepo := repository.NewMallGoodsRepo()
	thirdCategory := goodsRepo.GetGoodsCategoryById(goods.GoodsCategoryId)
	if thirdCategory.CategoryId != 0 {
		result["thirdCategory"] = thirdCategory
		secondCategory := goodsRepo.GetGoodsCategoryById(thirdCategory.CategoryId)
		if secondCategory.CategoryId != 0 {
			result["secondCategory"] = secondCategory
			firstCategory := goodsRepo.GetGoodsCategoryById(secondCategory.CategoryId)
			if firstCategory.CategoryId != 0 {
				result["firstCategory"] = firstCategory
			}
		}
	}
	response.Success(ctx, gin.H{"info": result}, "Success")
}

// BatchEdit 批量修改销售状态
func (receiver GoodsController) BatchEdit(ctx *gin.Context) {
	//取出管理员id
	user, _ := ctx.Get("adminUser")
	adminUserId := user.(model.NewBeeAdminUser).AdminUserId
	sellStatus, _ := strconv.Atoi(ctx.Params.ByName("status"))
	var param dto.BatchIdParam
	ctx.Bind(&param)
	if len(param.UserIds) < 1 {
		panic("参数异常")
	}
	if sellStatus != SellStatusUp && sellStatus != SellStatusDown {
		panic("状态异常")
	}
	//执行批量修改
	goodsRepo := repository.NewMallGoodsRepo()
	flag := goodsRepo.BatchUpdateSellStatus(param.UserIds, sellStatus, adminUserId)
	if flag {
		response.Success(ctx, nil, "修改成功")
	}
}

func NewGoodsController() GoodsController {
	db := common.GetDB()
	return GoodsController{DB: db}
}
